//package com.turing.post.organization.impl;
//
//import java.util.List;
//
//import javax.ejb.EJB;
//import javax.ejb.Local;
//import javax.ejb.Remote;
//import javax.ejb.Stateless;
//
//import com.turing.post.common.util.SysOperLog;
//import com.turing.post.common.util.SysOperLogDao;
//import com.turing.post.organization.util.IncomeBranch;
//import com.turing.post.organization.util.IncomeBranchDao;
//import com.turing.post.organization.util.OrganApplicationException;
//import com.turing.post.organization.util.OrganRespCode;
//import com.turing.post.organization.util.SaleCompany;
//import com.turing.post.organization.util.SaleCompanyBusiness;
//import com.turing.post.organization.util.SaleCompanyDao;
//import com.turing.post.organization.util.SysBranch;
//import com.turing.post.organization.util.SysBranchDao;
//import com.turing.post.organization.util.SysBranchSubord;
//import com.turing.post.organization.util.SysBranchSubordDao;
//import com.turing.post.util.PCPSApplicationException;
//import com.turing.post.util.PCPSUtil;
//import com.turing.post.util.PCPSWebInput;
//
///**
// * @author zxp
// * @Dec 18, 2012
// */
//@Stateless(mappedName = "organization.impl.SaleCompanyBusinessImpl")
//@Remote
//@Local
//public class SaleCompanyBusinessImpl implements SaleCompanyBusiness {
//
//	@EJB(mappedName = "organization.impl.SaleCompanyDaoImpl")
//	private SaleCompanyDao saleCompanyDaoImpl;
//
//	@EJB(mappedName = "organization.impl.IncomeBranchDaoImpl")
//	private IncomeBranchDao incomeBranchDaoImpl;
//
//	@EJB(mappedName = "organization.impl.SysBranchDaoImpl")
//	private SysBranchDao sysBranchDaoImpl;
//
//	@EJB(mappedName = "organization.impl.SysBranchSubordDaoImpl")
//	private SysBranchSubordDao sysBranchSubordDaoImpl;
//
//	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
//	private SysOperLogDao sysOperLogDaoImpl;
//
//	@Override
//	public void insertSaleCompanyInfo(PCPSWebInput webInput,
//			SaleCompany saleCompany) throws PCPSApplicationException {
//		// 登录用户机构号为空
//		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
//			throw new OrganApplicationException(
//					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
//		}
//		// 业务检查
//		if (saleCompany == null) {
//			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
//					"SaleCompany");
//		}
//		// 编号
//		if (PCPSUtil.isEmpty(saleCompany.getSaleCommpanyNo())) {
//			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
//					"SaleCommpanyNo");
//		}
//		// 名称
//		if (PCPSUtil.isEmpty(saleCompany.getSaleCompanyName())) {
//			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
//					"SaleCompanyName");
//		}
//		// 验证机构号是否已经使用
//		if (saleCompanyDaoImpl.getSaleCompanyBySaleCompanyNo(saleCompany
//				.getSaleCommpanyNo()) != null) {
//			throw new OrganApplicationException(
//					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
//					OrganRespCode.BRANCHNO_EXIST);
//		}
//		// 发卡商机构NO
//		if (PCPSUtil.isEmpty(saleCompany.getIssueBranchNo())) {
//			throw new OrganApplicationException(OrganRespCode.RC_ARGS_NULL, "",
//					"发卡商机构号IssueBranchNo为空!");
//		}
//
//		// 平台机构管理员归属机构号为空
//		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
//			// 操作员归属发卡商
//			String issueCompanyNo = PCPSUtil.getIssueCompanyNo(webInput);
//			// 设置隶属发卡商
//			saleCompany.setIssueCompanyNo(issueCompanyNo);
//			// IncomeBranchId 归属IssueCompanyNo
//			// SaleCommpanyNo 归属IssueCompanyNo
//			if (saleCompany.getIssueBranchNo().substring(0, 6).equals(
//					saleCompany.getIssueCompanyNo())
//					&& saleCompany.getSaleCommpanyNo().substring(0, 6).equals(
//							saleCompany.getIssueCompanyNo())) {
//			} else {
//				// 验证登录用户是否归属为当前发卡商
//				throw new OrganApplicationException(
//						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
//						OrganRespCode.ISSUE_INSERT_UPDATE, "无权添加销售商或销售商归属错误!");
//			}
//		} else {
//			throw new OrganApplicationException(
//					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION, "",
//					"超级管理员无法添加归属该发卡商下销售商!");
//		}
//
//		// 插入平台机构主键表数据
//		SysBranch sysBranch = new SysBranch(saleCompany.getSaleCommpanyNo(),
//				SysBranch.SYSBRANCH_SALECOMPANY);
//		sysBranchDaoImpl.insertSysBranch(sysBranch);
//		// 插入销售商
//		saleCompanyDaoImpl.insertSaleCompany(saleCompany);
//		//结构树数据
//		String issueBranchNo=saleCompany.getIssueBranchNo();
//		String saleCommpanyNo=saleCompany.getSaleCommpanyNo();
//		// 平台机构隶属关系
//		List<SysBranchSubord> sysBranchSubordList = sysBranchSubordDaoImpl
//				.getSysBranchSubordListBySubBranchNo(issueBranchNo);
//		// 插入所有父节点对应关系
//		SysBranchSubord sbs = null;
//		SysBranchSubord _sbs = null;
//		for (int i = 0; i < sysBranchSubordList.size(); i++) {
//			_sbs=sysBranchSubordList.get(i);
//			sbs = new SysBranchSubord();
//			sbs.setBranchNo(_sbs.getBranchNo());
//			sbs.setSubBranchNo(saleCommpanyNo);// 添加机构机构号
//			sbs.setSubBranchType(SysBranch.SYSBRANCH_SALECOMPANY);
//			sbs.setSubBranchSuperNo(issueBranchNo);// 上级机构号
//			sysBranchSubordDaoImpl.insertSysBranchSubord(sbs);
//		}
//
//		// 自己添加自己
//		SysBranchSubord sysBranchSub = new SysBranchSubord();
//		sysBranchSub.setBranchNo(saleCommpanyNo);
//		sysBranchSub.setSubBranchNo(saleCommpanyNo);
//		sysBranchSub.setSubBranchType(SysBranch.SYSBRANCH_SALECOMPANY);
//		sysBranchSubordDaoImpl.insertSysBranchSubord(sysBranchSub);
//		// 插入数据库操作日志
//		insertRecordingOperationLog(webInput,"创建销售商", saleCompany,
//				SysOperLog.operType_Add, "linkMan");
//	}
//
//	@Override
//	public void updateSaleCompanyInfo(PCPSWebInput webInput,
//			SaleCompany saleCompany) throws PCPSApplicationException {
//		// 登录用户机构号为空
//		if (PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
//			throw new OrganApplicationException(
//					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					OrganRespCode.ISSUE_INSERT_UPDATE, "登录用户机构号为NULL");
//		}
//		SaleCompany _saleCompany = saleCompanyDaoImpl
//				.getSaleCompanyBySaleCompanyNo(saleCompany.getSaleCommpanyNo());
//		// 业务检查
//		if (_saleCompany == null) {
//			throw new OrganApplicationException(
//					OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
//					OrganRespCode.ISSUE_INSERT_UPDATE, "待修改销售商数据库不存在!");
//		}
//		// 平台机构管理员归属机构号为空
//		if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
//			String operBranchNo = webInput.getOperBranchNo();
//			if (!operBranchNo.equals(_saleCompany.getSaleCommpanyNo()
//					.substring(0, operBranchNo.length()))) {
//				throw new OrganApplicationException(
//						OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
//						OrganRespCode.ISSUE_INSERT_UPDATE, "无权修改其他销售商!");
//			} else {
//				// IssueBranchNo 归属IssueCompanyNo
//				// SaleCommpanyNo 归属IssueCompanyNo
//				if (_saleCompany.getIssueBranchNo().substring(0, 6).equals(
//						_saleCompany.getIssueCompanyNo())
//						&& _saleCompany.getSaleCommpanyNo().substring(0, 6)
//								.equals(_saleCompany.getIssueCompanyNo())) {
//				} else {
//					// 验证登录用户是否归属为当前发卡商
//					throw new OrganApplicationException(
//							OrganApplicationException.ERRCODE_CATEGORY_APPLICATION,
//							OrganRespCode.ISSUE_INSERT_UPDATE,
//							"无权修改销售商或销售商归属错误!");
//				}
//			}
//		}
//		// 对象赋值
//		_saleCompany.setSaleCompanyName(saleCompany.getSaleCompanyName());
//		_saleCompany.setIncomeBranchId(saleCompany.getIncomeBranchId());
//		_saleCompany.setStatus(saleCompany.getStatus());
//		_saleCompany.setExpired(saleCompany.getExpired());
//		_saleCompany.setAddress(saleCompany.getAddress());
//		_saleCompany.setLinkMan(saleCompany.getLinkMan());
//		_saleCompany.setLinkTele(saleCompany.getLinkTele());
//
//		saleCompanyDaoImpl.updateSaleCompany(_saleCompany);
//		// 插入数据库操作日志
//		insertRecordingOperationLog(webInput,"修改销售商", saleCompany,
//				SysOperLog.operType_Edit, "linkMan");
//	}
//
//	@Override
//	public List<IncomeBranch> getIncomeBranchDropdownListByIssueCompanyNo(
//			String operBranchNo) {
//		String issueCompanyNo = operBranchNo.substring(0, 6);
//		return incomeBranchDaoImpl
//				.getIncomeBranchListByIssueCompanyNo(issueCompanyNo);
//	}
//
//	private void insertRecordingOperationLog(PCPSWebInput webInput,String logName,
//			SaleCompany obj, String operType, String lastFieldName)
//			throws PCPSApplicationException {
//		String logRemark = PCPSUtil.RecordingOperationLog(obj, lastFieldName);
//		if (!PCPSUtil.isEmpty(logRemark)) {
//			// 插入数据库日志
//			SysOperLog sysOperLog = new SysOperLog();
//			if (webInput != null && webInput.getOperId() != null) {
//				sysOperLog.setOperId(webInput.getOperId());
//				sysOperLog.setOperType(operType);
//				sysOperLog.setLogName(logName);
//				if (webInput != null) {
//					sysOperLog.setIpAddress(webInput.getIpAddress());
//				}
//				if (!PCPSUtil.isEmpty(webInput.getOperBranchNo())) {
//					sysOperLog.setIssueCompanyNo(webInput.getOperBranchNo()
//							.substring(0, 6));
//					sysOperLog.setBranchNo(webInput.getOperBranchNo());
//				}
//				sysOperLog.setRemark(logRemark);
//				sysOperLogDaoImpl.insertSysOperLog(sysOperLog);
//			}
//		}
//	}
//}
